package sv.gob.mh.dgii.dao.config;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@Profile("prod")
public class PersistenceHibernateConfigProd {

    Log logger = LogFactory.getLog(PersistenceHibernateConfigProd.class);

    @Bean
    public DataSource dataSource() throws Exception {
        Context ctx = new InitialContext();
        DataSource datasource = null;
        try {
            datasource = (DataSource) ctx.lookup("java:jboss/jdbc/desa");
        } catch (Exception e) {
        }
        if (datasource == null) {
            try {
                datasource = (DataSource) ctx.lookup("java:jboss/jdbc/desa");
            } catch (Exception e) {
            }
        }
        if (datasource == null) {
            datasource = (DataSource) ctx.lookup("jdbc/desa");
        }
        logger.info("SSC>PROD>dataSource: " + datasource);
        return datasource;
    }

    @Bean
    public DataSource dataSourceReports() throws Exception {
        Context ctx = new InitialContext();
        DataSource datasource = null;
        try {
            datasource = (DataSource) ctx.lookup("java:jboss/jdbc/jsiit/reports");
        } catch (Exception e) {
        }
        if (datasource == null) {
            try {
                datasource = (DataSource) ctx.lookup("java:jboss/jdbc/jsiit/reports");
            } catch (Exception e) {
            }
        }
        if (datasource == null) {
            datasource = (DataSource) ctx.lookup("jdbc/jsiit/reports");
        }
        logger.info("SSC>PROD>dataSourceReports: " + datasource);
        return datasource;
    }
}
